﻿using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Text;

namespace Jy123.Web.Module.TCenter
{
    public class TrainProps : IHttpHandler
    {
        private Jy123.Model.TCenter_Main TCenter;
        private Jy123.Model.Club_Main club;
        private Jy123.Model.Public_Level level;
        private Jy123.Model.User_Main user;
        public void ProcessRequest(HttpContext context)
        {
            string strUserIdFromCookie = Common.Cookie.getCookie("UserID");
            if (string.IsNullOrEmpty(strUserIdFromCookie))
            {
                context.Response.Redirect("/");
            }
            string key = "VenuesId";
            string venuesIdString = context.Request.QueryString[key];
            int venuesId = int.Parse(venuesIdString);

            Jy123.BLL.TCenter_Main BLLTCenter = new Jy123.BLL.TCenter_Main();
            Jy123.BLL.Club_Main BLLClub = new Jy123.BLL.Club_Main();
            Jy123.BLL.Public_Level BLLLevel = new Jy123.BLL.Public_Level();
            Jy123.BLL.User_Main BLLUser = new Jy123.BLL.User_Main();

            TCenter = BLLTCenter.GetModel(venuesId);
            user = BLLUser.GetModel(int.Parse(strUserIdFromCookie));
            level = BLLLevel.GetModel(TCenter.FLevel.Value);
            string mark = context.Request.QueryString["CMark"];
            if (string.IsNullOrEmpty(mark))
            {
                mark = context.Request["CMark"];
            }
            if (!string.IsNullOrEmpty(mark))
            {
                string strPropsId = context.Request.QueryString["PropsId"];
                Jy123.BLL.TCenter_ClubTC BLLClubTC = new Jy123.BLL.TCenter_ClubTC();
                string strGameWhere = string.Format("FTCenter = {0} AND TrainingTime IS NULL", TCenter.ID.ToString());
                string[] strClubId = TCenter.TrainClub.Split(',');
                int length = strClubId.Length;
                int index = (int.Parse(mark)) % length;
                int cId = int.Parse(strClubId[index]);
                Model.Club_Main club = BLLClub.GetModel(cId);
                if (!string.IsNullOrEmpty(strPropsId))
                {
                    int pId = int.Parse(strPropsId);

                    List<Jy123.Model.TCenter_ClubTC> listGame = BLLClubTC.GetModelList(strGameWhere);
                    if (listGame.Count > 0)
                    {
                        BLL.Props.Props BLLProps = new Jy123.BLL.Props.Props();
                        BLLProps.UsePropsToClub(user, club, listGame[0], pId);
                    }
                }
                string strResult = GetPropsString(context, club);
                if (strUserIdFromCookie.Equals(club.FUser.Value.ToString()))
                {
                    context.Response.Write(strResult);
                }
                else
                {
                    context.Response.Write("您不能对别人的俱乐部进行训练！");
                }

            }
        }

        private string GetPropsString(HttpContext context, Model.Club_Main club)
        {
            string propsString = context.Cache["venuesracepropsdiv"].ToString();
            BLL.Public_Props BLLProps = new Jy123.BLL.Public_Props();
            BLL.Props.Props BLLRecord = new Jy123.BLL.Props.Props();
            List<Model.Public_Props> listProps = BLLProps.GetModelList("FPType = 21");
            Dictionary<int, Model.Public_Props> dicProps = new Dictionary<int, Jy123.Model.Public_Props>();
            foreach (Model.Public_Props props in listProps)
            {
                dicProps.Add(props.ID, props);
            }
            List<Model.Club_PropsRecord> listRecords = BLLRecord.GetPropsRecord(user.ID, 0, "21");
            StringBuilder sbString = new StringBuilder();
            foreach (Model.Club_PropsRecord record in listRecords)
            {
                string strLi = context.Cache["venuesracepropsli"].ToString();
                Model.Public_Props cProps = dicProps[record.FProps.Value];
                strLi = strLi.Replace("$HiddenId$", "hidden" + cProps.ID);
                strLi = strLi.Replace("$Value$", cProps.ID.ToString());
                strLi = strLi.Replace("$ImageUrl$", cProps.Model);
                strLi = strLi.Replace("$ImageId$", "image" + cProps.ID.ToString());
                strLi = strLi.Replace("$Alt$", cProps.PName + " 数量：" + record.Quantity.ToString());
                StringBuilder sbClick = new StringBuilder();

                //正在比赛
                string lrInfo = "fillhtml('raceInfoDiv0','/Module/TCenter/TrainInfo.ashx?VenuesId=" + TCenter.ID.ToString() + "&Mark='+$('#cClubHidden').val()+'&random='+ Math.random()),";

                string lpInfo = "fillhtml('propsInfoDiv0','/Module/TCenter/TrainProps.ashx?VenuesId=" + TCenter.ID.ToString() + "&CMark='+$('#cClubHidden').val()+'&PropsId=" + record.FProps.Value.ToString() + "'+'&random='+ Math.random()),";
                string lcInfo = "fillhtml('clubInfoDiv0','/Module/TCenter/TrainClubInfo.ashx?VenuesId=" + TCenter.ID.ToString() + "&Mark='+$('#cClubHidden').val()+'&random='+ Math.random())";

                sbClick.Append("$(function(){");

                sbClick.Append(lpInfo);

                sbClick.Append(lrInfo);

                sbClick.Append(lcInfo);

                sbClick.Append("})");
                strLi = strLi.Replace("$Click$", sbClick.ToString());                
                strLi = strLi.Replace("$Action$", "进行训练");
                sbString.Append(strLi);
            }
            propsString = propsString.Replace("$Li$", sbString.ToString());
            return propsString;
        }


        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
